package net.zjitc.service.Impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.util.MissingFormatArgumentException;
import net.zjitc.dao.CategoryDao;
import net.zjitc.dao.ProductDao;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.redis.Redis;
import net.zjitc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/28 16:10
 * @description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {
  @Autowired
  private CategoryDao categorydao;
  @Autowired
  private Jedis jedis;
  @Autowired
  private Redis redis;
  @Autowired
  private ProductDao productDao;
  @Override
  public List<Category> findAll() {
    String string = jedis.get("category");
    Gson gson =new Gson();
    List<Category> categorylist =gson.fromJson(string,new TypeToken<List<Category>>(){}.getType());
    return categorylist;
  }

  @Override
  public List<CategorySecond> findAllSecond() {
    String string = jedis.get("categorysecond");
    Gson gson =new Gson();
    List<CategorySecond> categorySecondlist =gson.fromJson(string,new TypeToken<List<CategorySecond>>(){}.getType());
    return categorySecondlist;
  }

  @Override
  public Category findByCid(Integer cid) {
    String string = jedis.get("category");
    Gson gson =new Gson();
    List<Category> categorylist =gson.fromJson(string,new TypeToken<List<Category>>(){}.getType());
    for (Category c:categorylist){
      if (c.getCid().equals(cid)){
        return c;
      }
    }
    return null;
  }

  @Override
  public CategorySecond findByCsid(Integer csid) {
    String string = jedis.get("categorysecond");
    Gson gson =new Gson();
    List<CategorySecond> categorySecondList =gson.fromJson(string,new TypeToken<List<CategorySecond>>(){}.getType());
    for (CategorySecond c:categorySecondList){
      if (csid.equals(c.getCsid())){
        return c;
      }
    }
    return null;
  }

  @Override
  public Integer editCategory(Category category) {
    Integer integer=categorydao.editCategory(category);
    Gson gson =new Gson();
    List<Category> categoryList =categorydao.findAll();
    for (Category Category : categoryList){
      List<CategorySecond> CategorySecondList = categorydao.findCategorySecondByCid(category.getCid());
      for (CategorySecond categorySecond : CategorySecondList){
        List<Product> productList = productDao.findPageByCsid(categorySecond.getCsid());
        categorySecond.setProductList(productList);
      }
      Category.setSecondList(CategorySecondList);
    }
    String CategoryList =gson.toJson(categoryList);
    redis.set("category",CategoryList);
    return null;
  }

  @Override
  public Integer editCategorySecond(CategorySecond categorySecond) {
    Integer integer=categorydao.editCategorySecond(categorySecond);
    Gson gson = new Gson();
    List<CategorySecond> categorySecondList =categorydao.findAllSecond();
    String CategorysecondList =gson.toJson(categorySecondList);
    redis.set("categorysecond",CategorysecondList);
    return null;
  }

  @Override
  public Integer addCategory(String cname) {
    Integer integer=categorydao.addCategory(cname);
    Gson gson =new Gson();
    List<Category> categoryList =categorydao.findAll();
    for (Category category : categoryList){
      List<CategorySecond> categorySecondList = categorydao.findCategorySecondByCid(category.getCid());
      for (CategorySecond categorySecond : categorySecondList){
        List<Product> productList = productDao.findPageByCsid(categorySecond.getCsid());
        categorySecond.setProductList(productList);
      }
      category.setSecondList(categorySecondList);
    }
    String CategoryList =gson.toJson(categoryList);
    redis.set("category",CategoryList);
    return null;
  }


  @Override
  public Integer addCategorySecond(CategorySecond categorySecond) {
    Integer integer=categorydao.addCategorySecond(categorySecond);
    Gson gson = new Gson();
    List<CategorySecond> categorySecondList =categorydao.findAllSecond();
    String CategorysecondList =gson.toJson(categorySecondList);
    redis.set("categorysecond",CategorysecondList);
    return null;
  }

  @Override
  public Integer deleteCategory(String cid) {
    Integer integer =categorydao.deleteCategory(cid);
    Gson gson =new Gson();
    List<Category> categoryList =categorydao.findAll();
    for (Category category : categoryList){
      List<CategorySecond> categorySecondList = categorydao.findCategorySecondByCid(category.getCid());
      for (CategorySecond categorySecond : categorySecondList){
        List<Product> productList = productDao.findPageByCsid(categorySecond.getCsid());
        categorySecond.setProductList(productList);
      }
      category.setSecondList(categorySecondList);
    }
    String CategoryList =gson.toJson(categoryList);
    redis.set("category",CategoryList);
    return null;
  }

  @Override
  public Integer deleteCategorySecond(String csid) {
    Integer integer=categorydao.deleteCategorySecond(csid);
    Gson gson = new Gson();
    List<CategorySecond> categorySecondList =categorydao.findAllSecond();
    String CategorysecondList =gson.toJson(categorySecondList);
    redis.set("categorysecond",CategorysecondList);
    return null;
  }

}
